import { rowContextKey } from 'element-plus';
<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value =
            header_list[e].type == 'input_number' ? ['', ''] : ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div class="title">元数据列表</div>
        <div class="flex">
          <el-button type="primary" @click="show_box = true">新增</el-button>
          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        highlight-current-row
        v-loading="loading"
        :row-style="{
          height: height_size == 2 ? '50px' : '',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="名称"
          prop="name"
          key="名称"
          sortable
          v-if="header_list[0].showTable"
        >
        </el-table-column>
        <el-table-column
          label="编码"
          prop="code"
          key="编码"
          sortable
          v-if="header_list[1].showTable"
        >
        </el-table-column>
        <el-table-column
          label="是否计量"
          key="是否计量"
          sortable
          v-if="header_list[2].showTable"
        >
          <template #default="scope: any">
            <div v-if="scope.row.isMetering">是</div>
            <div v-else>否</div>
          </template>
        </el-table-column>
        <el-table-column
          label="是否规则"
          sortable
          key="是否规则"
          v-if="header_list[3].showTable"
        >
          <template #default="scope: any">
            <div v-if="scope.row.isRule">是</div>
            <div v-else>否</div>
          </template> </el-table-column
        ><el-table-column
          label="状态"
          key="状态"
          sortable
          v-if="header_list[4].showTable"
        >
          <template #default="scope: any">
            <div v-if="!scope.row.status">禁用</div>
            <div v-else>启用</div>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remark"
          key="备注"
          sortable
          show-overflow-tooltip
          v-if="header_list[5].showTable"
        >
        </el-table-column>
        <el-table-column label="操作" sortable show-overflow-tooltip>
          <template #default="scope: any">
            <el-button @click="edit_list({ item: scope.row, type: 1 })"
              >编辑</el-button
            >
            <el-button @click="edit_list({ item: scope.row, type: 2 })"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="m_10_0 pagination"
        v-model:currentPage="page_data.current"
        v-model:page-size="page_data.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="
          (e) => {
            page_data.pageSize = e
            get_list()
          }
        "
        @current-change="
          (e) => {
            page_data.current = e
            get_list()
          }
        "
      />
    </div>
    <el-drawer
      v-model="show_box"
      direction="rtl"
      size="30%"
      destroy-on-close
      @close="resetForm(addMetadataform)"
      :close-on-click-modal="false"
    >
      <template #title
        ><h4 v-if="add_form_data.id == ''">新增</h4>
        <h4 v-else>编辑</h4></template
      >
      <template #default>
        <el-form
          :model="add_form_data"
          :rules="rules"
          label-width="100px"
          ref="addMetadataform"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="add_form_data.name"></el-input>
          </el-form-item>
          <el-form-item label="编码" prop="code">
            <el-input v-model="add_form_data.code"></el-input>
          </el-form-item>
          <el-form-item label="是否计量">
            <el-radio-group v-model="add_form_data.isMetering">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否规则">
            <el-radio-group v-model="add_form_data.isRule">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="add_form_data.status">
              <el-radio :label="true">启用</el-radio>
              <el-radio :label="false">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="add_form_data.remark" type="textarea"></el-input>
          </el-form-item>
          <div class="tr m_top10">
            <el-button @click="resetForm(addMetadataform)">取消</el-button>
            <el-button
              type="primary"
              @click="submit(addMetadataform)"
              :loading="btn_loading"
              >确定</el-button
            >
          </div>
        </el-form>
      </template>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import { dictData, formatDate } from '@/utils/utils' // 权限校验
import search from '@/view/components/search.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted, computed } from 'vue'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import { useRoute } from 'vue-router'
import { FormInstance, ElMessage, ElMessageBox } from 'element-plus'
const route = useRoute()
const addMetadataform = ref<FormInstance>()
// 该页面权限组
const list = ref()
const total = ref()
const show_box = ref(false)
const loading = ref(false)
const height_size = ref(1)
const rules = ref({
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入编码', trigger: 'blur' }],
})
const header_list = ref<any>([
  {
    label: '名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '编号',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '是否计量',
    value: '',
    showTable: true,
    type: 'select',
    isShow: true,
    options: [
      {
        value: true,
        label: '是',
      },
      {
        value: false,
        label: '否',
      },
    ],
  },
  {
    label: '是否规则',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      {
        value: true,
        label: '是',
      },
      {
        value: false,
        label: '否',
      },
    ],
  },
  {
    label: '状态',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      {
        value: true,
        label: '启用',
      },
      {
        value: false,
        label: '禁用',
      },
    ],
  },
  {
    label: '备注',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
])
const page_data = ref<any>({
  current: 1,
  pageSize: 10,
  name: '',
  code: '',
  isMetering: '',
  isRule: '',
  status: '',
  remark: '',
})
const add_form_data = ref({
  id: '',
  name: '',
  code: '',
  isMetering: true,
  isRule: true,
  remark: '',
  status: true,
  sort: '',
})
const max_height = ref()
onMounted(() => {
  // 根据 路由 区分
  max_height.value = window.innerHeight - 320 // 不带分页的 table 高度
  EventBus.on('refresh', () => {
    get_list()
    ElMessage.success('刷新成功！')
  })
  get_list()
})
onUnmounted(() => {
  EventBus.off('refresh')
})
const btn_loading = ref(false)
const edit_list = (e) => {
  if (e.type == 1) {
    add_form_data.value = {
      id: e.item.id,
      name: e.item.name,
      code: e.item.code,
      isMetering: e.item.isMetering,
      isRule: e.item.isRule,
      remark: e.item.remark,
      status: e.item.status,
      sort: e.item.sort,
    }
    show_box.value = true
  } else {
    ElMessageBox.confirm('确定删除该记录？', '提示', {
      closeOnClickModal: false,
      type: 'warning',
    })
      .then(() => {
        axios
          .request({
            url: serviceApi.profileMetadata + '/' + e.item.id,
            method: 'delete',
          })
          .then(() => {
            ElMessage.success('操作成功！')
            setTimeout(() => {
              get_list()
            }, 1000)
          })
          .catch((err) => {
            ElMessage.error(err)
          })
      })
      .catch(() => {})
  }
}
const submit = async (formEl: FormInstance | undefined) => {
  await formEl.validate((valid) => {
    if (valid) {
      if (btn_loading.value) return
      btn_loading.value = true
      axios
        .request({
          url:
            serviceApi.profileMetadata +
            (add_form_data.value.id == '' ? '' : '/' + add_form_data.value.id),
          method: add_form_data.value.id == '' ? 'post' : 'put',
          data: add_form_data.value,
        })
        .then((res) => {
          console.log(res)
          ElMessage.success('操作成功！')
          setTimeout(() => {
            btn_loading.value = false
            resetForm(formEl)
            get_list()
          }, 1000)
        })
        .catch((err) => {
          btn_loading.value = false
          ElMessage.error(err)
        })
    }
  })
}
const get_list = () => {
  loading.value = true
  axios
    .request({
      url: serviceApi.profileMetadata + '/page',
      method: 'post',
      data: page_data.value,
    })
    .then((res) => {
      console.log(res)
      list.value = res.data.content
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((err) => {
      loading.value = false
      ElMessage.error(err)
    })
}
const search_key = () => {
  console.log(header_list.value)
  page_data.value = {
    current: 1,
    pageSize: 10,
    name: header_list.value[0].value,
    code: header_list.value[1].value,
    isMetering:
      header_list.value[2].value == ''
        ? ''
        : header_list.value[2].value
        ? true
        : false,
    isRule:
      header_list.value[3].value == ''
        ? ''
        : header_list.value[3].value
        ? true
        : false,
    status:
      header_list.value[4].value == ''
        ? ''
        : header_list.value[4].value
        ? true
        : false,
    remark: header_list.value[5].value,
  }
  get_list()
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  add_form_data.value = {
    id: '',
    name: '',
    code: '',
    isMetering: true,
    isRule: true,
    status: true,
    remark: '',
    sort: '',
  }
  show_box.value = false
}
</script>

<style lang="scss" scoped>
.response_data {
  max-width: 500px !important;
  overflow-x: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
:deep(.el-table__expanded-cell) {
  background-color: #f0f2f5;
  padding-left: 100px;
  div {
    padding: 5px 0;
  }
  .dian {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 5px;
    &:first-child {
      background-color: #3580eb;
    }
  }
}
.title {
  font-weight: bold;
  span {
    cursor: pointer;
    padding: 10px;
  }
  .select {
    color: #3580eb;
  }
}
</style>
